﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RM2012Service.DataService;
using RM2012Service.DataService;
using RM2012Service.Business;
using System.Data;
using System.Data.SqlClient;

namespace RM2012Service.DataAccess
{
    public class CustomerDAO
    {
        DBConnection db;

        public CustomerDAO()
        {
            db = new DBConnection();
        }

        public DataSet GetAllCustomer()
        {
            return db.ExecuteDataSet("GetAllCustomer", "Customer", null);
        }

        public DataTable GetCustomerLast()
        {
            return db.ExecuteDataTable("GetCustomerLast", null);
        }

        public DataSet GetCustomerLastDataSet()
        {
            return db.ExecuteDataSet("GetCustomerLast", "Customer", null);
        }

        public int Add(CustomerInfo info)
        {
            SqlParameter[] param = new SqlParameter[10];
            param[0] = new SqlParameter("@CusId", SqlDbType.VarChar, 6);
            param[0].Value = info.CusId;

            param[1] = new SqlParameter("@FullName", SqlDbType.NVarChar, 100);
            param[1].Value = info.FullName;

            param[2] = new SqlParameter("@Sex", SqlDbType.Bit);
            param[2].Value = info.Sex;

            param[3] = new SqlParameter("@DiscountPer", SqlDbType.Money);
            param[3].Value = info.DiscountPer;

            param[4] = new SqlParameter("@Debt", SqlDbType.Money);
            param[4].Value = info.Debt;

            param[5] = new SqlParameter("@Address", SqlDbType.NVarChar, 200);
            param[5].Value = info.Address;

            param[6] = new SqlParameter("@Phone", SqlDbType.NVarChar, 100);
            param[6].Value = info.Phone;

            param[7] = new SqlParameter("@Email", SqlDbType.NVarChar, 100);
            param[7].Value = info.Email;

            param[8] = new SqlParameter("@State", SqlDbType.NVarChar, 100);
            param[8].Value = info.State;

            param[9] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[9].Value = info.Remark;

            return db.ExecuteNoneQuery("AddCustomer", param);
        }

        public int Update(CustomerInfo info)
        {
            SqlParameter[] param = new SqlParameter[10];
            param[0] = new SqlParameter("@CusId", SqlDbType.VarChar, 6);
            param[0].Value = info.CusId;

            param[1] = new SqlParameter("@FullName", SqlDbType.NVarChar, 100);
            param[1].Value = info.FullName;

            param[2] = new SqlParameter("@Sex", SqlDbType.Bit);
            param[2].Value = info.Sex;

            param[3] = new SqlParameter("@DiscountPer", SqlDbType.Money);
            param[3].Value = info.DiscountPer;

            param[4] = new SqlParameter("@Debt", SqlDbType.Money);
            param[4].Value = info.Debt;

            param[5] = new SqlParameter("@Address", SqlDbType.NVarChar, 200);
            param[5].Value = info.Address;

            param[6] = new SqlParameter("@Phone", SqlDbType.NVarChar, 100);
            param[6].Value = info.Phone;

            param[7] = new SqlParameter("@Email", SqlDbType.NVarChar, 100);
            param[7].Value = info.Email;

            param[8] = new SqlParameter("@State", SqlDbType.NVarChar, 100);
            param[8].Value = info.State;

            param[9] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[9].Value = info.Remark;

            return db.ExecuteNoneQuery("UpdateCustomer", param);
        }

        public int UpdateSex(String cusId)
        {
            SqlParameter param = new SqlParameter("@CusId", SqlDbType.VarChar, 6);
            param.Value = cusId;

            return db.ExecuteNoneQuery("UpdateCustomerSex", param);
        }
    }
}
